package com.xxxx.rpc.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxx.api.service.GoodsService;
import com.xxxx.api.pojo.Goods;
import com.xxxx.api.pojo.GoodsExample;
import com.xxxx.api.utils.RespBean;
import com.xxxx.rpc.mapper.GoodsMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @Author : ymd
 * @Date : 2020/11/5 0005 11:16
 * @Version 1.0
 * @Description : 商品服务实现类
 */
@Service(version = "1.0")
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public RespBean selectGoods(Goods goods, Integer pageNum, Integer pageSize) {
        //定义key的形式数组
        String[] goodsKey={
                "goods:pageNum_"+pageNum+"pageSize_"+pageSize+":",
                "catId_:",
                "brandId_:",
                "goodsName_:"
        };
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        //新建查询条件
        GoodsExample example = new GoodsExample();
        GoodsExample.Criteria criteria = example.createCriteria();
        //分类参数
        if (null!=goods.getCatId()&&0!=goods.getCatId()){
            criteria.andCatIdEqualTo(goods.getCatId());
            goodsKey[1]="catId_"+goods.getCatId()+":";
        }
        //品牌参数
        if (null!=goods.getBrandId()&&0!=goods.getBrandId()){
            criteria.andBrandIdEqualTo(goods.getBrandId());
            goodsKey[2]="brandId_"+goods.getBrandId()+":";
        }
        //关键词
        if (!StringUtils.isEmpty(goods.getGoodsName())){
            criteria.andGoodsNameLike("%"+goods.getGoodsName()+"%");
            goodsKey[3]="goodsName_"+goods.getGoodsName()+":";
        }
        //获得完整key
        String collect = Arrays.stream(goodsKey).collect(Collectors.joining());
        ValueOperations<String,String> opsForValue = redisTemplate.opsForValue();
        //获取redis中的数据
        String infoGoods = opsForValue.get(collect);
        if (!StringUtils.isEmpty(infoGoods)){
            return RespBean.success("查询成功！", JSON.parseObject(infoGoods, PageInfo.class));
        }
        //查询结果
        List<Goods> list=goodsMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)){
            PageInfo<Goods> pageInfo=new PageInfo<>(list);
            //放入缓存
            opsForValue.set(collect, JSON.toJSONString(pageInfo));
            return RespBean.success("查询成功！", pageInfo);
        }else {
            //为空，则放入空缓存
            opsForValue.set(collect, JSON.toJSONString(new PageInfo<>(new ArrayList<Goods>())),60, TimeUnit.SECONDS);
        }
        return RespBean.error("查询失败！");
    }
}
